package chpt10.texteditor;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginFrame extends JFrame {
	private JTextField usernameField;
	private JPasswordField passwordField;

	public LoginFrame() {
		setTitle("登录");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(300, 200);
		setLocationRelativeTo(null);

		JPanel panel = new JPanel(new GridLayout(2, 2));

		JLabel usernameLabel = new JLabel("用户名:");
		usernameField = new JTextField();
		JLabel passwordLabel = new JLabel("密码:");
		passwordField = new JPasswordField();

		panel.add(usernameLabel);
		panel.add(usernameField);
		panel.add(passwordLabel);
		panel.add(passwordField);

		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		JButton loginButton = new JButton("登录");
		JButton cancelButton = new JButton("取消");
		buttonPanel.add(loginButton);
		buttonPanel.add(cancelButton);

		add(panel, BorderLayout.CENTER);
		add(buttonPanel, BorderLayout.SOUTH);

		loginButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String username = usernameField.getText();
				String password = new String(passwordField.getPassword());
				if (username.equals("admin") && password.equals("password")) {
					new EditorFrame().setVisible(true);
					dispose();
				} else {
					JOptionPane.showMessageDialog(LoginFrame.this, "用户名密码错误", "错误", JOptionPane.ERROR_MESSAGE);
				}
			}
		});

		cancelButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// 取消按钮的操作，例如关闭登录窗口
				dispose();
			}

		});
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				JFrame.setDefaultLookAndFeelDecorated(true);
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
				} catch (Exception e) {
					e.printStackTrace();
				}
				new LoginFrame().setVisible(true);
			}
		});
	}
}